home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr37 / satsfaxt.zip / CCPUTILS.ZIP / CCSAVEF.C < prev    next >
C/C++ Source or Header  |  1990-01-30  |  12KB  |  267 lines

  1. /*----------------------------------------------------------------------------*
  2.  *                 CCSAVEF.C                      *
  3.  *----------------------------------------------------------------------------*
  4.  * The program allows the user to move or fax received from an Intel          *
  5.  * Connection CoProcessor or Group III fax machine to a normal MS-DOS          *
  6.  * file. The file can be moved to any pre-existing path on any disk drive.    *
  7.  *                                          *
  8.  * The following source code is intended to assist developers in          *
  9.  * creating applications which support the  DCA/Intel Communicating          *
  10.  * Applications Specification Version 1.0A. It is provided free of charge     *
  11.  * and on an as-is basis. THE IMPLIED WARRENTIES OF MERCHANTABILITY AND       *
  12.  * FITNESS FOR A PARTICULAR PURPOSE ARE SPECIFICALLY EXCLUDED. This source    *
  13.  * code may be modified, enhanced, copied and distributed with applications   *
  14.  * that support CAS on a royalty free basis.                      *
  15.  *----------------------------------------------------------------------------*/
  16. #include <stdio.h>
  17. #include <dos.h>
  18. #include <io.h>
  19. #include <malloc.h>
  20. #include <string.h>
  21. #include "cas.h"            /* Intel CAS header file.             */
  22. #include "parse.h"            /* Command line parser header file.      */
  23. #include "util.h"            /* CAS utilities header file.         */
  24.  
  25. int JobNo = 0;                /* Job number to move.             */
  26. int Help = FALSE;            /* Show help text.                 */
  27. int RedirOut = FALSE;            /* Redirect output to file.          */
  28. int Delete = FALSE;            /* Delete after moving.             */
  29.  
  30. ECF *TCFbuffer;             /* Task Control File buffer.         */
  31. FTR *FTRbuffer;             /* File Transfer Record buffer.         */
  32. FILE *ccout;                /* Redirected stdout file.             */
  33.  
  34. STABLE SwitchTable [] =         /* Command line parser switch table.     */
  35. {
  36.     { 'D', BOOL, &Delete   },
  37.     { 'd', BOOL, &Delete   },
  38.     { 'E', INT,  &JobNo    },
  39.     { 'e', INT,  &JobNo    },
  40.     { 'R', BOOL, &RedirOut },
  41.     { 'r', BOOL, &RedirOut },
  42.     { '?', BOOL, &Help       }
  43. };
  44. #define TABLESIZE (sizeof(SwitchTable)/sizeof(STABLE))
  45.  
  46. char *DescTable[] =            /* Help function description table. */
  47. {
  48.     "CCSAVEF (V1.0) Save a fax received from a Group III fax machine as the file\n",
  49.     "specified by \"filename\". \"filename\" may include a path (for example,\n",
  50.     "c:\wp\sales.fax.\n\n",
  51.     "/E<number. save the event specified by <number> (for example, /e16041). CCAM\n",
  52.     "(Connection CoProcessor Application Manager) assigns numbers to the events\n",
  53.     "it processes. You can see the numbers of all send and receive events by typing\n",
  54.     "CCSTAT with no options.\n\n",
  55.     "Syntax: CCSAVEF /E<number> filename [option]\n\n"
  56. };
  57. #define DESCSIZE (sizeof(DescTable)/sizeof(DescTable[0]))
  58.  
  59. char *HelpTable[] =            /* Help function switch table. */
  60. {
  61.      "  /D          Delete event after moving files.\n\n",
  62.      "  /R          Redirect output to file OUTPUT.CC.\n",
  63.      "  /?          Display help information.\n"
  64. };
  65. #define HELPSIZE (sizeof(HelpTable)/sizeof(HelpTable[0]))
  66.  
  67. /*---------------------------------------------------------------------------*
  68.  *                   GetTaskBuffer()                     *
  69.  *---------------------------------------------------------------------------*
  70.  * Get information about the event specified by Handle. The Task Control     *
  71.  * File is opened and read. The FileHandle returned is a normal MS-DOS file  *
  72.  * handle.                                     *
  73.  *---------------------------------------------------------------------------*
  74.  * Parameters:    int Handle - Event handle to return information from.         *
  75.  *        BYTE Queue - Queue (Task, Log or Receive) to get event from. *
  76.  *        char *TCFbuffer - Uninitialized ECS structure, filled with   *
  77.  *            task info on return.                     *
  78.  *        int *FileHandle - Filled with DOS file handle on return.     *
  79.  * Return:    none                                 *
  80.  *---------------------------------------------------------------------------*/
  81. GetTaskBuffer(int Handle, BYTE Queue, char *TCFbuffer, int *FileHandle)
  82. {
  83.     /* Let CAS open the Task Control File. Read the contents into TCFbuffer. */
  84.     if((*FileHandle = CASOpenFile(Handle,0,Queue)) > 0) {
  85.         read(*FileHandle, TCFbuffer, sizeof(ECF));
  86.     }
  87.     else
  88.     CASError(CASNOTCF, TRUE, -(*FileHandle));
  89. }
  90.  
  91. /*---------------------------------------------------------------------------*
  92.  *               GetFileTransferBuffer()                 *
  93.  *---------------------------------------------------------------------------*
  94.  * Get information about the File Transfer Record. Offset specifies the      *
  95.  * location within the Task Control File of the FTR record. Usually, the     *
  96.  * first FTR is at offset 383 in the TCF (if no cover text). In any case,    *
  97.  * the offset of the first FTR is stored at offset 10 of the TCF. Each         *
  98.  * subsequent FTR is 128 bytes after the first.                  *
  99.  *---------------------------------------------------------------------------*
  100.  * Parameters:    long Offset - File offset to begin reading.             *
  101.  *        char *FTRbuffer - Uninitialized FTR structure, filled with   *
  102.  *            file transfer info on return.                 *
  103.  *        int *FileHandle - DOS file handle obtained from a call to    *
  104.  *            CASOpenFile().                         *
  105.  * Return:    none                                 *
  106.  *---------------------------------------------------------------------------*/
  107. GetFileTransferBuffer(long Offset, char *FTRbuffer, int *FileHandle)
  108. {
  109.     /* Read the File Transfer Record specified by Offset. */
  110.     lseek(*FileHandle, Offset, SEEK_SET);
  111.     read(*FileHandle, FTRbuffer, sizeof(FTR));
  112. }
  113.  
  114. /*---------------------------------------------------------------------------*
  115.  *                 OkayToMove()                     *
  116.  *---------------------------------------------------------------------------*
  117.  * OkayToMove checks for existence of FileName. If the file does not exist,  *
  118.  * the function returns TRUE.  If it exists, CAS Move Received File will     *
  119.  * fail, so the user is prompted for permission to overwrite. If permission  *
  120.  * is granted, the file is removed and the function returns true.  If the    *
  121.  * removal fails or the user does not give permission, the function returns  *
  122.  * false.                                     *
  123.  *---------------------------------------------------------------------------*
  124.  * Parameters:    char *FileName - Path and name of file to test.          *
  125.  * Return:    TRUE if it is okay to call CASMoveReceivedFile, FALSE         *
  126.  *            otherwise.                             *
  127.  *---------------------------------------------------------------------------*/
  128. int OkToMove (char *FileName)
  129. {
  130.     char answer[1];            /* Users response to confirmation quest. */
  131.  
  132.     /* Check to see if file already exists. */
  133.     if(!access(FileName,0)) {
  134.     /* Confirm overwrite on existing file. */
  135.     fprintf(stderr,"    File %s already exists - overwrite? (y/n)", strupr(FileName));
  136.     gets(answer);
  137.  
  138.     if(toupper(answer[0]) == 'Y') {
  139.         /* If we can't remove the existing file, don't try saving fax. */
  140.         if(remove(FileName) == -1) {
  141.         CASError(CASDELJOB, FALSE, 0);
  142.         return(FALSE);
  143.         }
  144.  
  145.         /* OK, save the fax as planned. */
  146.         return(TRUE);
  147.     }
  148.     /* No permission to overwrite. */
  149.     else
  150.         return(FALSE);
  151.     }
  152.     /* File doesn't already exist - go ahead and save fax. */
  153.     else
  154.     return(TRUE);
  155. }
  156.  
  157. /*---------------------------------------------------------------------------*
  158.  *                 MoveFile()                     *
  159.  *---------------------------------------------------------------------------*
  160.  * Move all faxes associated with the event specified by EventHandle. The    *
  161.  * faxes are moved to the path NewFilePath given on the command line. Each   *
  162.  * fax is named as the last componant of the specified path name. Therefore, *
  163.  * the command line must include a fully qualified file name.             *
  164.  *---------------------------------------------------------------------------*
  165.  * Parameters:    int EventHandle - Event handle containing the file to move.  *
  166.  *        char *NewFilePath - The drive and path where file is to be   *
  167.  *            move. The last componant is the file name.             *
  168.  *        ECF *TCFbuffer - Uninitialized ECF structure, filled with    *
  169.  *            Task Control info in return.                 *
  170.  *        FTR *FTRbuffer - Uninitialized FTR structure, filled with    *
  171.  *            file transfer info on return.                 *
  172.  * Return:    none                                 *
  173.  *---------------------------------------------------------------------------*/
  174. MoveFile(int EventHandle, char *NewFilePath, ECF *TCFbuffer, FTR *FTRbuffer)
  175. {
  176.     int ErrorCode;            /* Error return from CAS function.         */
  177.     long Offset;            /* Offset of FTR in task control file.   */
  178.     int FileHandle;            /* DOS file handle (from GetTaskBuffer). */
  179.  
  180.     /* Get information about the task. */
  181.     GetTaskBuffer(EventHandle, RECEIVE_QUEUE, (char *)TCFbuffer, &FileHandle);
  182.  
  183.     /* The FTR is at the offset stored at FTROffset in the TCF.
  184.        This will vary depending on if cover text was sent. */
  185.     Offset = TCFbuffer->FTROffset;
  186.  
  187.     /* Get information about the fax that was transferred. */
  188.     GetFileTransferBuffer(Offset, (char *)FTRbuffer, &FileHandle);
  189.  
  190.     /* Error - this routine only for FAXes. */
  191.     if(TCFbuffer->TransferType == FILE_TRANSFER) 
  192.     CASError(CASNOTFAX, TRUE, 0);
  193.         
  194.     /* Move the received fax to the new path and file name. */
  195.     if(OkToMove(NewFilePath))
  196.     if((ErrorCode = CASMoveReceivedFile(EventHandle, 1, NewFilePath)) != SUCCESS) {
  197.         CASError(CASMOVE, FALSE, -ErrorCode);
  198.         /* Make sure we don't delete the event! */
  199.         Delete = FALSE;
  200.         }
  201.  
  202.     close(FileHandle);
  203.  
  204.     if(Delete)
  205.     CASDeleteFile(EventHandle, 0, LOG_QUEUE);
  206. }
  207.  
  208. /*---------------------------------------------------------------------------*
  209.  *                    Main                     *
  210.  *---------------------------------------------------------------------------*
  211.  * Move all faxes from a receive event to the specified directory.         *
  212.  * The user specifies the directory and filename on the command line.         *
  213.  *---------------------------------------------------------------------------*
  214.  * Return: The DOS exit code is 0 if no errors are encountered, 1 for         *
  215.  *       commandline syntax errors, 2 for CCAM error, and 3 for DOS         *
  216.  *       errors.                                 *
  217.  *---------------------------------------------------------------------------*/
  218. main(int argc, char **argv)
  219. {
  220.  
  221.     int result;             /* DOS & CAS function return value.      */
  222.  
  223.     argc = ParseCommand(argc, argv, SwitchTable, TABLESIZE);
  224.  
  225.     /* If help is wanted, print info and exit (with 0 exit code). */
  226.     if(Help || (argc == 1))
  227.     CASHelp(DescTable, DESCSIZE, HelpTable, HELPSIZE);
  228.  
  229.     /* Insure that the Resident Scheduler is installed. */
  230.     if((result = CASGetInstalledState()) != INSTALLED) {
  231.     if( result == NOTiOK )
  232.         CASError(CASNOHWO, TRUE, 0);
  233.     else
  234.         CASError(CASNOHWN, TRUE, 0);
  235.     }
  236.  
  237.     /* Redirect stdout to a file. */
  238.     if(RedirOut) {
  239.     if((ccout = fopen("OUTPUT.CC", "w")) == NULL)
  240.         CASError(CASOPENTEMP, TRUE, 0);
  241.     if(-1 == dup2(fileno(ccout), 1))
  242.         CASError(CASBADREDIR, TRUE, 0);
  243.     }
  244.  
  245.     /* Check to make sure that a (one only) path name was specified. */
  246.     if(argc < 2)
  247.     CASError(CASARGPATH, TRUE, 0);
  248.     else if(argc > 2)
  249.     CASError(CASARGMULTPATH, TRUE, 0);
  250.  
  251.     /* Get buffer for Task information. */
  252.     TCFbuffer = (ECF *)malloc(sizeof(ECF));
  253.     if(TCFbuffer == NULL)
  254.     CASError(CASNOMEM, TRUE, 0);
  255.  
  256.     /* Get buffer for File Transfer information. */
  257.     FTRbuffer = (FTR *)malloc(sizeof(FTR));
  258.     if(FTRbuffer == NULL)
  259.     CASError(CASNOMEM, TRUE, 0);
  260.  
  261.     /* Point to path name and move it! */
  262.     argv++;
  263.     MoveFile(JobNo, *argv, TCFbuffer, FTRbuffer);
  264.  
  265.     exit(0);
  266. }
  267.